<?php

/**
 * This is the model class for table "user".
 */
Yii::import('SiteManagement.models.base.FrontDeskUserBase');
class FrontDeskUser extends FrontDeskUserBase
{
    const STATUS_DEACTIVE = 0, STATUS_ACTIVE = 1;
    
    public $confirmPassword;
    public $passwordOld;
    public $verifyCode;
    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        return array(
            array('password, email', 'required', 'on'=>'register, login'),
            array('password', 'required', 'on'=>'reset, change_password'),
            
            array('username', 'required', 'on'=>'register'),
            array('username, email', 'unique', 'on'=>'register'),
            array('email', 'email', 'on'=>'register, login'),
            //array('username', 'filter', 'filter'=>'strtolower', 'on'=>'register'),
            //array('username', 'match', 'pattern'=>'/^[a-z0-9_]{6,50}$/', 'on'=>'register'),
            array('password', 'length', 'min'=>5),
            array('confirmPassword', 'compare', 'compareAttribute' => 'password', 'on'=>'register, reset, change_password'),
            
            array('email', 'exist', 'on'=>'forgot'),
            
            array('passwordOld', 'required', 'on'=>'change_password'),
            
            array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(), 'on'=>'forgot_confirm'),
            
            array('validation_code, validation_type, validation_expired, status, created_date, last_login', 'safe'),
            array('id, username, password, email, created_date, last_login, validation_code, validation_type, validation_expired, status', 'safe', 'on'=>'search'),
        );
    }
    
    public function getStatusOptions()
    {
        return array(
            self::STATUS_DEACTIVE => 'Deactive',
            self::STATUS_ACTIVE => 'Active',
        );
    }
    
    public function getStatusText()
    {
        $options = $this->getStatusOptions();
        return isset($options[$this->status]) ? $options[$this->status] : Yii::t('app', 'Unknown {att}', array('{att}'=>$this->status));
    }

    /**
     * This method is invoked after saving a record successfully.
     * The default implementation raises the {@link onAfterSave} event.
     * You may override this method to do postprocessing after record saving.
     * Make sure you call the parent implementation so that the event is raised properly.
     */
    protected function beforeSave()
    {
        
        if ($this->getIsNewRecord()) {
            $this->created_date = date('Y-m-d H:i:s');
            if (!isset($this->status))
                $this->status = self::STATUS_ACTIVE;
        }
        return parent::beforeSave();
    }
    
    /**
     * This method is invoked before validation starts.
     * The default implementation calls {@link onBeforeValidate} to raise an event.
     * You may override this method to do preliminary checks before validation.
     * Make sure the parent implementation is invoked so that the event can be raised.
     * @return boolean whether validation should be executed. Defaults to true.
     * If false is returned, the validation will stop and the model is considered invalid.
     */
    protected function beforeValidate()
    {
        $this->username = strtolower($this->username);
        return parent::beforeValidate();
    }
}